﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.DO;
using SES.CMS.BL;
using Telerik.Web.UI;
using Telerik.Web.UI.Editor.DialogControls;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Data;

namespace SES.CMS.AdminCP
{
    public partial class User : System.Web.UI.Page
    {
        sysUserDO objUser = new sysUserDO();
        PhanQuyenDO objPhanquyen = new PhanQuyenDO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //if (Session["UserId"] != null)
                view();
            }
        }
        void view()
        {
            if (Request.QueryString["UserId"] != null)
            {
                objUser.UserID = Convert.ToInt32(Request.QueryString["UserId"].ToString());
                objUser = new sysUserBL().Select(objUser);
            }
            details.Visible = true;
            if (Request.QueryString["UserId"] != null)
            {
                mkhau.Visible = true;
                ltUsername.ReadOnly = true;
            }
            else
                mkhau.Visible = false;
            Session["pas"] = objUser.Password;
            ltAddress.Text = objUser.Address;
            ltEmail.Text = objUser.Email;
            ltFullname.Text = objUser.FullName;
            ltUsername.Text = objUser.Username;
            ltYahoo.Text = objUser.YahooIM;
            ddlPhanquyen.SelectedValue = Convert.ToString(objUser.UserType);
            if (ddlPhanquyen.SelectedValue == "1")
            {
                pnPhanquyen.Visible = true;
                rptMenuDatasource();
                int UserId = Convert.ToInt32(Request.QueryString["UserId"].ToString());
                DataTable dtMenuAdmin = new PhanQuyenBL().SelectGetByTop("", "UserID=" + UserId, "");
                if (dtMenuAdmin.Rows.Count > 0)
                    for (int i = 0; i < dtMenuAdmin.Rows.Count; i++)
                    {
                        for (int j = 0; j < chkListMenu.Items.Count; j++)
                        {
                            if (int.Parse(chkListMenu.Items[j].Value) == int.Parse(dtMenuAdmin.Rows[i]["MenuAdminID"].ToString()))
                            {
                                chkListMenu.Items[j].Selected = true;
                                break;
                            }
                        }
                    }

            }
            if (objUser.IsActive == true)
                rdCheck.Checked = true;
            else
                rdCheck1.Checked = true;
        }
        protected void InitObject()
        {
            objUser = new sysUserBL().Select(objUser);
            if (Request.QueryString["UserId"] != null)
            {
                objUser.UserID = Convert.ToInt32(Request.QueryString["UserId"].ToString());
            }

            objUser.Username = ltUsername.Text;
            objUser.FullName = ltFullname.Text;
            objUser.YahooIM = ltYahoo.Text;
            objUser.Email = ltEmail.Text;
            objUser.Address = ltAddress.Text;
            if (rdCheck.Checked)
                objUser.IsActive = true;
            else if (rdCheck1.Checked)
                objUser.IsActive = false;
            if (ddlPhanquyen.SelectedValue == "0")

                objUser.UserType = 1;
            else
                objUser.UserType = 2;
        }
        protected void btnCapnhat_Click(object sender, EventArgs e)
        {
            InitObject();

            //}
            if (objUser.UserID <= 0)
            {
                DataTable dtUser = new sysUserBL().CheckUser(ltUsername.Text.Trim());
                if (dtUser.Rows.Count == 0)
                {
                    objUser.Password = Functions.EncryptMd5(ltPassword.Text.Trim());
                      
                    int UserId = new sysUserBL().Insert(objUser);
                    if (ddlPhanquyen.SelectedValue != "0")
                    {
                        DataTable dtMenuAdmin = new MenuAdminsBL().SelectGetByTop("", "ParentID=0", "");
                        for (int i = 0; i < dtMenuAdmin.Rows.Count; i++)
                        {
                            if (chkListMenu.Items[i].Selected)
                                objPhanquyen.UserID = UserId;
                            objPhanquyen.MenuAdminID = Convert.ToInt32(chkListMenu.Items[i].Value);
                            new PhanQuyenBL().Insert(objPhanquyen);
                           
                        }
                    }
                    Functions.Alert("Thêm mới thành công!!!", "ListUser.aspx");
                }
                else
                {
                    Functions.Alert("Tên đăng nhập đã tồn tại!!!");
                }
            }
            else
            {
                string pas = Session["pas"].ToString();
                if (ltOldPas.Text != "")
                {
                    
                        DataTable dtPass = new sysUserBL().CheckPass(Functions.EncryptMd5(ltOldPas.Text.Trim()));
                        if (dtPass.Rows.Count > 0)
                        {
                            if (ltPassrepeat.Text.Trim() != "")
                            {
                                objUser.Password = Functions.EncryptMd5(ltPassword.Text.Trim());
                                new PhanQuyenBL().Delete(int.Parse(Request.QueryString["UserId"].ToString()));

                                new sysUserBL().Update(objUser);

                                DataTable dtMenuAdmin = new MenuAdminsBL().SelectGetByTop("", "ParentID=0", "");
                                for (int i = 0; i < dtMenuAdmin.Rows.Count; i++)
                                {
                                    if (chkListMenu.Items[i].Selected)
                                    {
                                        objPhanquyen.UserID = Convert.ToInt32(Request.QueryString["UserId"].ToString());
                                        objPhanquyen.MenuAdminID = Convert.ToInt32(chkListMenu.Items[i].Value);
                                        new PhanQuyenBL().Insert(objPhanquyen);
                                    }
                                }
                                Functions.Alert("Thay đổi thành công!!!", "ListUser.aspx");

                            }
                            else
                            {
                                Functions.Alert("Bạn chưa nhập mật khẩu!!!");
                                return;
                            }
                            objUser.Password = Functions.EncryptMd5(ltPassword.Text.Trim());
                        }
                        else
                        {
                            Functions.Alert("Mật khẩu cũ không đúng!!!");
                        }
                   
                }

                else
                {

                    objUser.Password = Session["pas"].ToString();

                    new PhanQuyenBL().Delete(int.Parse(Request.QueryString["UserId"].ToString()));

                    new sysUserBL().Update(objUser);

                    DataTable dtMenuAdmin = new MenuAdminsBL().SelectGetByTop("", "ParentID=0", "");
                    for (int i = 0; i < dtMenuAdmin.Rows.Count; i++)
                    {
                        if (chkListMenu.Items[i].Selected)
                        {
                            objPhanquyen.UserID = Convert.ToInt32(Request.QueryString["UserId"].ToString());
                            objPhanquyen.MenuAdminID = Convert.ToInt32(chkListMenu.Items[i].Value);
                            new PhanQuyenBL().Insert(objPhanquyen);
                        }
                    }
                    Functions.Alert("Thay đổi thành công!!!", "ListUser.aspx");

                }

            }
        }


        protected void btnLamLai_Click(object sender, EventArgs e)
        {
            ltAddress.Text = "";
            ltEmail.Text = "";
            ltFullname.Text = "";
            ltPassrepeat.Text = "";
            ltPassword.Text = "";
            ltUsername.Text = "";
            ltUsername.Focus();
            ltYahoo.Text = "";
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListUser.aspx");
        }

        protected void ddlPhanquyen_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlPhanquyen.SelectedValue == "0")
                pnPhanquyen.Visible = false;
            else
                pnPhanquyen.Visible = true;
            rptMenuDatasource();
        }

        private void rptMenuDatasource()
        {
            //rptMenu.DataSource = new MenuAdminsBL().SelectGetByTop("", "len(Level)=5", "");
            //rptMenu.DataBind();
            chkListMenu.DataSource = new MenuAdminsBL().SelectGetByTop("", "ParentID=0", "");
            chkListMenu.DataBind();
        }
        protected string FriendlyUrl(string s)
        {
            return Ultility.Change_AV(s);
        }
    }
}